/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////// Custom Functions Start                                                                             */
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/



/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////// Variables Start                                                                                    */
var $ = jQuery.noConflict();

var generalAnimationSpeed = 450; // animations speed, this value is used by almost all animations

var frameLoadComplete = false;
var frameIsAdapted = false;

var listIsOpen = false; // intial list status
var tempListMargin = 0; // initial items list position
var headerHeight = 64; // the height of the header
var slideVal = 0; // initial slide value
var itemWidth = 231; // the total width of an item, including it's border and margins
var listWidth = 0;
var listWrapWidth = 0;


/*///////////////// Variables End                                                                                         */
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/



/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*//////////////////// Document Ready Starts                                                                              */
jQuery(document).ready(function ($) {


    // initial settings start
    var tempHeight = jQuery(window).height(); // initial window height
    var tempWidth = jQuery(window).width(); // initial window width

    var fullHeight = jQuery(window).height(); // initial browser height
    var fullWidth = jQuery(window).width(); // initial browser height

    var currentSize = jQuery('#selectThemeBtn').attr("data-initialSize"); // initial window mode
    // initial settings end

    // adjust items list size starts
    $(".itemsList").width($(".itemsList .item").length * itemWidth - 15);
    // adjust items list size ends


    // initial item settings start
    if ($("#selectThemeBtn").text() == "Select an item...") {
        $(".item:first-child").addClass("currentItem");
    }

    $(".item:last-child").css({"margin-right": "10px"});
    // sinitial item settings end


    // get the default item details starts
    //var itemSizes = $("#selectThemeBtn").attr("data-itemSizes").split("&");

    // check if the device size is available ends
    // get the default item details ends


    // set size function starts   
    function fixFrameSize() {

        if (currentSize == "fullSize") {
            /*$("#iframeWrap").css({"height": ($(window).height() - headerHeight )});
             $("#iframeWrap").css({"width": $(window).width()});
             $(".fullIcon").addClass("currentMode");

             tempWidth = $(window).width();
             tempHeight = $(window).height()-headerHeight;

             adaptFrame();
             */
        }

        if (currentSize == "smartPhoneP") {
            $("#iframeWrap").css({"height": smartPHeight});
            $("#iframeWrap").css({"width": smartPWidth});
            $(".smartIconPortrait").addClass("currentMode");

            tempWidth = smartPWidth;
            tempHeight = smartPHeight;

            adaptFrame();
        }

        if (currentSize == "smartPhoneL") {
            $("#iframeWrap").css({"height": smartLHeight});
            $("#iframeWrap").css({"width": smartLWidth});
            $(".smartIconLandscape").addClass("currentMode");

            tempWidth = smartLWidth;
            tempHeight = smartLHeight;

            adaptFrame();
        }

        if (currentSize == "tabletP") {
            $("#iframeWrap").css({"height": tabletPHeight});
            $("#iframeWrap").css({"width": tabletPWidth});
            $(".tabletIconPortrait").addClass("currentMode");

            tempWidth = tabletPWidth;
            tempHeight = tabletPHeight;

            adaptFrame();
        }

        if (currentSize == "tabletL") {
            $("#iframeWrap").css({"height": tabletLHeight});
            $("#iframeWrap").css({"width": tabletLWidth});
            $(".tabletIconlandscape").addClass("currentMode");

            tempWidth = tabletLWidth;
            tempHeight = tabletLHeight;

            adaptFrame();
        }

        $(".iframeOuterWrap").css({"height": ($(window).height() - headerHeight )});
        $(".iframeOuterWrap").css({"width": $(window).width()});

    }

    fixFrameSize();
    // set size function ends   


    // window resize function starts			
    $(window).resize(function () {

        frameIsAdapted = true;
        fixFrameSize();

        fullWidth = $(window).width();
        fullHeight = $(window).height();

        if (currentSize == "fullSize") {
            $("#iframeWrap").stop(false, false).animate({
                marginLeft: (fullWidth - $("#iframeWrap").width()) / 2
            }, 250, 'easeInOutQuint');
        }


        listWidth = $(".itemsList").width();
        listWrapWidth = $(".itemsListWrap").width();

        if (listWidth + tempListMargin - listWrapWidth <= 0 && listWidth > listWrapWidth) {
            tempListMargin = -(listWidth - listWrapWidth);
            $(".itemsList").css({"left": tempListMargin});
        } else if (listWidth <= listWrapWidth) {
            tempListMargin = 0;
            $(".itemsList").css({"left": 0});
        }

    });
    // window resize function ends 


    // close the list starts
    function closeDrop() {

        if (listIsOpen == true) {

            $(".itemsListControlsWrap").stop(false, false).animate({
                marginTop: -$(".itemsListControlsWrap").height()
            }, generalAnimationSpeed, 'easeInOutQuint', function () {

                $("#selectThemeBtn").removeClass("selectThemeBtnDown");

            });

            listIsOpen = false;

        } else {

            $(".itemsListControlsWrap").stop(false, false).animate({
                marginTop: 0
            }, generalAnimationSpeed, 'easeInOutQuint');

            $("#selectThemeBtn").addClass("selectThemeBtnDown");

            listIsOpen = true;

        }
    };
    // close the list ends


    // select theme/template button function starts			
    $("#selectThemeBtn").click(function () {

        closeDrop();

        return false;

    });
    // select theme/template button function ends	


    // items list controls start
    $(".itemsListRight").click(function () {

        listWidth = $(".itemsList").width();
        listWrapWidth = $(".itemsListWrap").width();

        if (listWrapWidth <= listWidth && (listWidth - listWrapWidth + tempListMargin) >= listWrapWidth) {
            slideVal = listWrapWidth;

        } else if (listWrapWidth <= listWidth && (listWidth - listWrapWidth + tempListMargin) > 0) {
            slideVal = listWidth - listWrapWidth + tempListMargin;
        } else {
            slideVal = 0;
        }

        tempListMargin = tempListMargin - slideVal;

        $(".itemsList").stop(false, false).animate({

            left: tempListMargin
        }, generalAnimationSpeed, 'easeInOutQuint');

        return false;
    });


    $(".itemsListLeft").click(function () {

        listWidth = $(".itemsList").width();
        listWrapWidth = $(".itemsListWrap").width();

        if (tempListMargin < 0 && -tempListMargin >= listWrapWidth) {
            slideVal = -listWrapWidth;

        } else if (tempListMargin < 0 && -tempListMargin < listWrapWidth) {
            slideVal = tempListMargin;
        } else {
            slideVal = 0;
        }

        tempListMargin = tempListMargin - slideVal;

        $(".itemsList").stop(false, false).animate({

            left: tempListMargin
        }, generalAnimationSpeed, 'easeInOutQuint');

        return false;
    });
    // items list controls end


    // template/theme selected function	starts		
    $(".item").click(function () {

        loadingInProgress();
        iframeLoaded();

        $(".currentItem").removeClass("currentItem");
        $(this).addClass("currentItem");

        var itemTitle = $(this).attr("data-itemTitle");
        var itemLink = $(this).attr("data-itemLink");
        var itemInitialSize = $(this).attr("data-initialSize");
        var itemSizes = $(this).attr("data-itemSizes").split("&");
        var itemPurchaseUrl = $(this).attr("href");

        currentSize = itemInitialSize;

        function checkModes() {
            $(".currentMode").removeClass("currentMode");

            if (currentSize == "fullSize") {
                $(".fullIcon").addClass("currentMode");
                tempWidth = $(window).width();
                tempHeight = $(window).height() - headerHeight;
            }
            if (currentSize == "smartPhoneP") {
                $(".smartIconPortrait").addClass("currentMode");
                tempWidth = smartPWidth;
                tempHeight = smartPHeight;
            }
            if (currentSize == "smartPhoneL") {
                $(".smartIconLandscape").addClass("currentMode");
                tempWidth = smartLWidth;
                tempHeight = smartLHeight;
            }
            if (currentSize == "tabletP") {
                $(".tabletIconPortrait").addClass("currentMode");
                tempWidth = tabletPWidth;
                tempHeight = tabletPHeight;
            }
            if (currentSize == "tabletL") {
                $(".tabletIconLandscape").addClass("currentMode");
                tempWidth = tabletLWidth;
                tempHeight = tabletLHeight;
            }

            frameIsAdapted = false;
            adaptFrame();
        }

        checkModes();


        return false;
    });
    // template/theme selected function	ends	


    // close the list on click outside starts (won't work on iframe)
    $('html').click(function () {
        if (listIsOpen == true) {
            closeDrop();
        }
        ;
    });
    // close the list on click outside ends


});